package cn.etgps.etgps.utils;


import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;


/**
 * Created by itte on 2016/4/1.
 */
public class GetTimeUtils {
    private static final int MAX_TIME_DURATION = 1000 * 60 * 60 * 24;
    final Calendar mTemCalender = Calendar.getInstance();
    /**
     * 开始时间date类型
     */
    /**
     * 系统时间Canlendar类型
     */
    private Calendar mSystemCalendar = null;
    /**
     * 设置选择小时分钟的变量 小时
     */
    private int mHour = 0;
    /**
     * 设置选择小时分钟的变量 分钟
     */
    private int mMinute = 0;
    private Date mStartDate = null;
    private long mEndDate;
    private long endDate;
    private Context context;
    private Date time;

    public GetTimeUtils(Date date, Context context) {
        this.context = context;
        mSystemCalendar = AppUtils.getCurrentSystemDate();
        mStartDate = mSystemCalendar.getTime();
        mEndDate = mSystemCalendar.getTime().getTime();
        endDate = mEndDate;
        if (date != null) {
            mStartDate = date;
        }
    }


    /**
     * 根据类型得到开始时间的相应年月日
     */
    public void getYearMonthDay(final TextView mYear, final String type, Date date) {
        if (date != null) {
            mTemCalender.setTime(date);//把开始date转化为Calendar
        } else {
            mTemCalender.setTime(mStartDate);//把开始date转化为Calendar
        }
        DatePickerDialog datePicker = new DatePickerDialog(
                context,
                new DatePickerDialog
                        .OnDateSetListener() {
                    @Override
                    public void onDateSet(
                            DatePicker view,
                            int year,
                            int monthOfYear,
                            int dayOfMonth) {
                        mTemCalender.set(year, monthOfYear, dayOfMonth);

                        mStartDate = mTemCalender.getTime();  //把Calendar转换为开始时间
                        if (mStartDate.getTime() > System.currentTimeMillis()) {
                            mStartDate = mSystemCalendar.getTime();
                            mTemCalender.setTime(mStartDate);
                            Toast.makeText(context, "时间不能大于今天", Toast.LENGTH_SHORT).show();
                        }
                        mYear.setText(AppUtils.getCurrentDate(mTemCalender.getTime()));
                        if (type.equals("log")) {
                            if ((endDate - mStartDate.getTime()) > MAX_TIME_DURATION || (endDate
                                    < mStartDate.getTime())) {
                                mStartDate.setTime(endDate - MAX_TIME_DURATION);
                                mYear.setText(AppUtils.getCurrentDate(mStartDate));
                                Toast.makeText(context, "时间不能大于1天", Toast.LENGTH_SHORT).show();

                            }
                        }
                        setDate(mStartDate);
                    }

                },
                mTemCalender.get(Calendar.YEAR),
                mTemCalender.get(Calendar.MONTH),
                mTemCalender.get(Calendar.DATE));
        datePicker.show();
    }

    public Date getDate() {
        return time;
    }

    private void setDate(Date date) {
        this.time = date;
    }

    /**
     * 根据类型得到相应小时分钟
     */
    public void getHourMintue(final TextView mTime, Date date) {
        if (date != null) {
            mTemCalender.setTime(date);//把开始date转化为Calendar
        } else {
            mTemCalender.setTime(mStartDate);//把开始date转化为Calendar
        }
        mHour = mTemCalender.get(Calendar.HOUR_OF_DAY);  //时间结束的要得到当前时间
        mMinute = mTemCalender.get(Calendar.MINUTE);
        final TimePickerDialog time1 = new TimePickerDialog(context, new TimePickerDialog
                .OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mTemCalender.set(
                        mTemCalender.get(Calendar.YEAR),
                        mTemCalender.get(Calendar.MONTH),
                        mTemCalender.get(Calendar.DATE),
                        hourOfDay,
                        minute);

                mStartDate = mTemCalender.getTime();  //把Calendar转换为开始时间
                mTime.setText(AppUtils.getCurrentTime(mTemCalender.getTime()));
                mMinute = minute;
                mHour = hourOfDay;
                setDate(mStartDate);
                //第一次进来开始时间为0：0 之后使用选择的时间 所以用变量
            }
        }, mHour, mMinute, true);
        time1.show();
    }
}